package com.ocom.aiot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.aiot.dao.IDaoMjDeviceInfo;
import com.ocom.aiot.service.MjDeviceInfoService;
import com.ocom.aiot.vo.accessdevice.AddDeviceInfoVo;
import com.ocom.aiot.vo.accessdevice.GetDeviceInfoVo;
import com.ocom.aiot.vo.accessdevice.SetDeviceStatusVo;
import com.ocom.common.entity.aiot.EntityMjDeviceInfo;
import com.ocom.common.entity.aiot.EntityXfDeviceInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MjDeviceInfoServiceImpl extends ServiceImpl<IDaoMjDeviceInfo, EntityMjDeviceInfo> implements MjDeviceInfoService {

    @Autowired
    private IDaoMjDeviceInfo daoMjDeviceInfo;

    @Override
    public EntityMjDeviceInfo GetDeviceList(String machNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        return daoMjDeviceInfo.selectOne(query);

    }

    @Override
    public void updateAppVer(Long comId, String machNo, String versionCode) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
		update.eq("com_id",comId);
		update.eq("device_no",machNo);
		update.set("app_ver",versionCode);
		this.update(update);
    }

    // 2.2.1.获取设备信息（getDeviceInfo）
    @Override
    public List<EntityMjDeviceInfo> AccessDeviceGetDeviceInfo(GetDeviceInfoVo getDeviceInfoVo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        if (StringUtils.isNotEmpty(getDeviceInfoVo.getSaasNo())){
            query.eq("saas_no",getDeviceInfoVo.getSaasNo());
            query.or();
            query.eq("p_saas_no",getDeviceInfoVo.getSaasNo());
        }
        if (StringUtils.isNotNull(getDeviceInfoVo.getAgentId())){
            query.eq("agent_id",getDeviceInfoVo.getAgentId());
            query.or();
            query.eq("p_agent_id",getDeviceInfoVo.getAgentId());
        }
        if (StringUtils.isNotNull(getDeviceInfoVo.getComId())){
            query.eq("com_id",getDeviceInfoVo.getComId());
            query.or();
            query.eq("p_com_id",getDeviceInfoVo.getComId());
        }
        if (StringUtils.isNotEmpty(getDeviceInfoVo.getDeviceNo())){
            query.eq("device_no",getDeviceInfoVo.getDeviceNo());
        }
        if (StringUtils.isNotNull(getDeviceInfoVo.getDeviceType())){
            query.eq("device_type",getDeviceInfoVo.getDeviceType());
        }
        if (StringUtils.isNotNull(getDeviceInfoVo.getDeviceVerType())){
            query.eq("device_ver_type",getDeviceInfoVo.getDeviceVerType());
        }
        if (StringUtils.isNotEmpty(getDeviceInfoVo.getAppVer())){
            query.eq("app_ver",getDeviceInfoVo.getAppVer());
        }
        if (StringUtils.isNotNull(getDeviceInfoVo.getStatus())){
            query.eq("status",getDeviceInfoVo.getStatus());
        }
        PageHelper.startPage(getDeviceInfoVo.getPage(), getDeviceInfoVo.getSize());
        return daoMjDeviceInfo.selectList(query);
    }

    // 2.3.2.添加设备信息（addDeviceInfo）
    @Override
    public void AccessDeviceAddDeviceInfo(AddDeviceInfoVo addDeviceInfoVo) {
        EntityMjDeviceInfo entityXfDeviceInfo = new EntityMjDeviceInfo();


        if (StringUtils.isNotNull(addDeviceInfoVo.getPSaasNo())){
            entityXfDeviceInfo.setPSaasNo(addDeviceInfoVo.getPSaasNo());
        }
        if (StringUtils.isNotNull(addDeviceInfoVo.getPComId())){
            entityXfDeviceInfo.setPComId(addDeviceInfoVo.getPComId());
        }

        if (StringUtils.isNotNull(addDeviceInfoVo.getPAgentId())){
            entityXfDeviceInfo.setPAgentId(addDeviceInfoVo.getPAgentId());
        }



        if (StringUtils.isNotNull(addDeviceInfoVo.getDeviceNo())){
            entityXfDeviceInfo.setDeviceNo(addDeviceInfoVo.getDeviceNo());
        }

        if (StringUtils.isNotNull(addDeviceInfoVo.getDeviceType())){
            entityXfDeviceInfo.setDeviceType(addDeviceInfoVo.getDeviceType());
        }
        if (StringUtils.isNotNull(addDeviceInfoVo.getDeviceNo())){
            entityXfDeviceInfo.setDeviceNo(addDeviceInfoVo.getDeviceNo());
        }



        if (StringUtils.isNotNull(addDeviceInfoVo.getCommMode())){
            entityXfDeviceInfo.setCommMode(addDeviceInfoVo.getCommMode());
        }

        if (StringUtils.isNotNull(addDeviceInfoVo.getEndTime())){
            entityXfDeviceInfo.setEndTime(addDeviceInfoVo.getEndTime());
        }

        if (StringUtils.isNotNull(addDeviceInfoVo.getDeviceVerType())){
            entityXfDeviceInfo.setDeviceVerType(addDeviceInfoVo.getDeviceVerType());
        }

        entityXfDeviceInfo.setStatus(0L);

        daoMjDeviceInfo.insert(entityXfDeviceInfo);
    }

    //判断设备编号
    @Override
    public EntityMjDeviceInfo GetDeviceByDeviceNo(String deviceNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("device_no",deviceNo);
        return daoMjDeviceInfo.selectOne(query);
    }


    // 2.3.3.修改设备信息（updateDeviceInfo）
    @Override
    public void AccessDeviceUpdateDeviceInfo(EntityMjDeviceInfo entityMjDeviceInfo) {


        daoMjDeviceInfo.updateById(entityMjDeviceInfo);
    }

    ////2.3.4.删除设备信息（deleteDeviceInfo）
    @Override
    public EntityMjDeviceInfo AccessDeviceDeleteDeviceInfo(String deviceNo,Integer force) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("device_no", deviceNo);
        if (force == 0) {
            query.eq("status", 0);
        }
        daoMjDeviceInfo.delete(query);
        QueryWrapper<EntityMjDeviceInfo> queryWrapper = Wrappers.query();
        queryWrapper.eq("device_no", deviceNo);
        if (force == 0) {
            queryWrapper.in("status", 1, 2);
        }
        return daoMjDeviceInfo.selectOne(queryWrapper);
    }

    //修改最后通讯时间
    @Override
    public void UpdateCommTime(String machNo) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("device_no",machNo);
        EntityXfDeviceInfo entityXfDeviceInfo = new EntityXfDeviceInfo();
        entityXfDeviceInfo.setCommTime(new Date());
        query.set("comm_time",entityXfDeviceInfo.getCommTime());
        query.set("update_time",entityXfDeviceInfo.getUpdateTime());
        this.update(query);
    }

    // 2.4.5.SaaS平台设置设备状态（SetDeviceStatus）
    @Override
    public void AccessDeviceSetDeviceStatus(SetDeviceStatusVo setDeviceStatusVo) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("device_no",setDeviceStatusVo.getDeviceNo());
        EntityXfDeviceInfo entityXfDeviceInfo = new EntityXfDeviceInfo();
        if (StringUtils.isNotEmpty(setDeviceStatusVo.getSaasNo())){
            query.set("saas_no",setDeviceStatusVo.getSaasNo());
        }
        if (StringUtils.isNotNull(setDeviceStatusVo.getComId())){
            query.set("com_id",setDeviceStatusVo.getComId());
        }
        if (StringUtils.isNotNull(setDeviceStatusVo.getAgentId())){
            query.set("agent_id",setDeviceStatusVo.getAgentId());
        }
        query.set("status",setDeviceStatusVo.getStatus());
        entityXfDeviceInfo.setUpdateTime(new Date());
        query.set("update_time",entityXfDeviceInfo.getUpdateTime());
        this.update(query);
    }

    //1、从总库设备信息表mj_device_list找到device_no=machNo的记录
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjDeviceInfo selectDeviceInfo(String machNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("device_no",machNo);
        query.last("limit 1");
        return this.getOne(query);
    }

    //修改通信协议和最后通信时间
    @Override
    public void updateCommTimeCommMode(String machNo, Integer commMode) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("device_no",machNo);
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setCommTime(new Date());
        query.set("comm_time",entityMjDeviceInfo.getCommTime());
        query.set("comm_mode",commMode);
        this.update(query);
    }


    @Override
    public void updateAppVerLast(Long companyId,String machNo,  String AppVerTab) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotNull(AppVerTab)){
            query.eq("app_ver_tab",AppVerTab);
        }
        query.set("app_ver_last",AppVerTab);
        this.update(query);
    }

    //修改Jobid数据
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateJobId(Long companyId, String machNo, Long jobId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
//        if (StringUtils.isNotNull(companyId)){
//            query.eq("com_id",companyId);
//        }
//        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
//        }

        query.set("app_job_id",jobId);
        this.update(query);
    }
}
